package com.eleadmin.order.biz.impl;

import com.eleadmin.common.system.entity.User;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.order.biz.SettingsBiz;
import com.eleadmin.order.entity.Settings;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.order.result.SelectResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-03-31 11:23:58
 */
@Repository
public class SettingsBizImpl extends BaseDao implements SettingsBiz {

    @Override
    public JsonResult list(PageResult2<T> page, Settings settings, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Settings> rm = new BeanPropertyRowMapper<Settings>(Settings.class);
        StringBuffer sql = new StringBuffer("select s.id,s.organization_id,s.name");
        sql.append(",s.english_name,s.pinyin,s.value_m,s.lon_la");
        sql.append(",s.type_m,s.state,s.remark,s.create_time");
        sql.append(",s.create_user,s.update_time,s.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_settings s ");
        sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=s.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=s.organization_id and sogzn.deleted=0 ");
        sql.append(" where s.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and s.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(settings)) {
            if (Utils.isNotEmpty(settings.getName())) {
                sql.append(" and (instr(s.name,:name)>0 or instr(s.pinyin,:name)>0) ");
                map.put("name", settings.getName());
            }
            if (Utils.isNotEmpty(settings.getTypeM())) {
                sql.append(" and s.type_m=:type_m ");
                map.put("type_m", settings.getTypeM());
            }
            if (Utils.isNotEmpty(settings.getSort()) && Utils.isNotEmpty(settings.getOrder())) {
                sql.append(" order by s." + settings.getSort() + " " + settings.getOrder());
            } else {
                sql.append(" order by s.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Settings> list(Settings settings, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Settings> rm = new BeanPropertyRowMapper<Settings>(Settings.class);
        StringBuffer sql = new StringBuffer("select s.id,s.organization_id,s.name");
        sql.append(",s.english_name,s.pinyin,s.value_m,s.lon_la");
        sql.append(",s.type_m,s.state,s.remark,s.create_time");
        sql.append(",s.create_user,s.update_time,s.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_settings s ");
        sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=s.organization_id and sogzn.deleted=0 ");
        sql.append(" where s.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and s.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(settings)) {
            if (Utils.isNotEmpty(settings.getName())) {
                sql.append(" and (instr(s.name,:name)>0 or instr(s.pinyin,:name)>0) ");
                map.put("name", settings.getName());
            }
            if (Utils.isNotEmpty(settings.getTypeM())) {
                sql.append(" and s.type_m=:type_m ");
                map.put("type_m", settings.getTypeM());
            }
            if (Utils.isNotEmpty(settings.getSort()) && Utils.isNotEmpty(settings.getOrder())) {
                sql.append(" order by s." + settings.getSort() + " " + settings.getOrder());
            } else {
                sql.append(" order by s.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSel(Settings settings, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select s.value_m value, s.name label, s.english_name");
        sql.append(" from chf_settings s ");
        sql.append(" where s.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and s.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(settings)) {
            if (Utils.isNotEmpty(settings.getName())) {
                sql.append(" and instr(s.name,:name)>0 ");
                map.put("name", settings.getName());
            }
            if (Utils.isNotEmpty(settings.getTypeM())) {
                sql.append(" and s.type_m=:type_m ");
                map.put("type_m", settings.getTypeM());
            }
            if (Utils.isNotEmpty(settings.getSort()) && Utils.isNotEmpty(settings.getOrder())) {
                sql.append(" order by s." + settings.getSort() + " " + settings.getOrder());
            } else {
                sql.append(" order by s.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Settings getById(String id) {
        RowMapper<Settings> rm = new BeanPropertyRowMapper<Settings>(Settings.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select s.id,s.organization_id,s.name");
        sql.append(",s.english_name,s.pinyin,s.value_m,s.lon_la");
        sql.append(",s.type_m,s.state,s.remark,s.create_time");
        sql.append(",s.create_user,s.update_time,s.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_settings s ");
        sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=s.organization_id and sogzn.deleted=0 ");
        sql.append(" where s.deleted=0 and s.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(Settings settings, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_settings (");
        sql.append("organization_id,name,english_name,pinyin");
        sql.append(",value_m,lon_la,type_m,state");
        sql.append(",remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:english_name,:pinyin");
        sql.append(",:value_m,:lon_la,:type_m,:state");
        sql.append(",:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", settings.getName());
        map.put("english_name", settings.getEnglishName());
        map.put("pinyin", settings.getPinyin());
        map.put("value_m", settings.getValueM());
        map.put("lon_la", settings.getLonLa());
        map.put("type_m", settings.getTypeM());
        map.put("state", settings.getState());
        map.put("remark", settings.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Settings settings, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_settings set ");
        sql.append("name=:name,english_name=:english_name,pinyin=:pinyin ");
        sql.append(",value_m=:value_m,lon_la=:lon_la,type_m=:type_m ");
        sql.append(",state=:state,remark=:remark,update_time=now() ");
        sql.append(",update_user=:update_user where id=:id ");
        map.put("name", settings.getName());
        map.put("english_name", settings.getEnglishName());
        map.put("pinyin", settings.getPinyin());
        map.put("value_m", settings.getValueM());
        map.put("lon_la", settings.getLonLa());
        map.put("type_m", settings.getTypeM());
        map.put("state", settings.getState());
        map.put("remark", settings.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", settings.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_settings set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
